//
//  ZYXClassVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/7.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXClassVC.h"

#import "ZYXClassTabCell.h"

#import "ZYXVideoPlayVC.h"
#import "ZYXClassificationModel.h"
@interface ZYXClassVC ()
@property (strong,nonatomic) NSMutableArray *dataArr;
///页数
@property (strong,nonatomic) NSString *page;
///类型
@property (strong,nonatomic) NSString *nameStr;

@end

@implementation ZYXClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.oneBtn.selected = YES;
  
}
- (IBAction)clickOneBtn:(id)sender {
    self.oneBtn.selected = YES;
    self.twoBtn.selected = NO;
    self.threeBtn.selected = NO;
       self.nameStr = @"";
    [self.dataArr removeAllObjects];
    [self loadRequestPage:@"1"];
}
- (IBAction)clickTwoBtn:(UIButton *)sender {
    self.oneBtn.selected = NO;
    self.twoBtn.selected = YES;
    self.threeBtn.selected = NO;
       self.nameStr = @"collection";
     [self.dataArr removeAllObjects];
    [self loadRequestPage:@"1"];
   
}
- (IBAction)clickThreeBtn:(UIButton *)sender {
    self.oneBtn.selected = NO;
    self.twoBtn.selected = NO;
    self.threeBtn.selected = YES;
     self.nameStr = @"attention";
     [self.dataArr removeAllObjects];
    [self loadRequestPage:@"1"];
   
}
#pragma mark MainUI
-(void)setupViews{
    self.tableView.frame = CGRectMake(0,HEIGHT(48), SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-HEIGHT(48));
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXClassTabCell" bundle:nil] forCellReuseIdentifier:@"ZYXClassTabCell"];
    [self.view addSubview:self.tableView];
    [self setupRefresh];
      self.nameStr = @"";
    self.page = @"1";
    [self loadRequestPage:@"1"];
    
}
#pragma mark 加载数据
- (void)setupRefresh {
   
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
         [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    if ([UID integerValue] ==0) {
        [ZYXLoginViewController setupLogin];
        return;
    }
    [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_FenleiTwo) parmas:@{@"fid":self.mid,@"name":self.nameStr,@"page":page} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            if ([self.page integerValue] <= [responseObject[@"data"][@"last_page"] integerValue]) {
            
                    NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
                    [self.dataArr addObjectsFromArray:modelArr];
                    [self.tableView reloadData];
                
            }else{
               [JKToast showWithText:@"暂无加载更多内容"];
            }
         
       
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        [self endRefresh];
        
    } failureBlock:^(NSError *error) {
           [self endRefresh];
    }];
}


#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXClassTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXClassTabCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(100);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击进入详细课程");
    if ([UID integerValue] >0) {
        ZYXClassificationModel *model = self.dataArr[indexPath.row];
        ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
        vc.title = model.title;
        vc.zid = model.uid;
        [self.navigationController pushViewController:vc animated:YES];
    }else{

          [ZYXLoginViewController setupLogin];
        
    }

   
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
